package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.AreaMapper;
import com.winshare.saleAssistant.domain.entity.AreaDO;
import com.winshare.saleAssistant.domain.dto.req.AreaCreateDTO;
import com.winshare.saleAssistant.domain.dto.req.AreaDeleteDTO;
import com.winshare.saleAssistant.domain.dto.req.AreaUpdateDTO;
import com.winshare.saleAssistant.domain.dto.resp.AreaRespDTO;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.AreaMapStruct;
import com.winshare.saleAssistant.service.AreaService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    private final AreaMapper areaMapper;

    public AreaServiceImpl(AreaMapper areaMapper) {
        this.areaMapper = areaMapper;
    }

    @Override
    public void save(AreaCreateDTO areaCreateDTO) {
        AreaDO areaDO = AreaMapStruct.INSTANCE.fromAreaCreateDTO(areaCreateDTO);
        areaMapper.saveArea(areaDO);
    }

    @Override
    public void update(AreaUpdateDTO areaUpdateDTO) {
        AreaDO areaDO = AreaMapStruct.INSTANCE.fromAreaUpdateDTO(areaUpdateDTO);
        areaMapper.updateArea(areaDO);
    }

    @Override
    public void delete(AreaDeleteDTO areaDeleteDTO) {
        AreaDO areaDO = AreaMapStruct.INSTANCE.fromAreaDeleteDTO(areaDeleteDTO);
        if(existChildArea(areaDO)) {
            throw new BusinessException("该节点不能删除，因为还存在子节点");
        }
        if(userByRegion(areaDO)) {
            throw new BusinessException("当前区域已被大区使用,帮不能被删除");
        }
        areaMapper.deleteArea(areaDO);
    }

    private boolean userByRegion(AreaDO areaDO) {
        return areaMapper.countByRegionArea(areaDO) > 0;
    }

    @Override
    public List<AreaRespDTO> getAreaList() {
        List<AreaRespDTO> areaRespDTOList = areaMapper.getAllAreaList();
        if(!CollectionUtils.isEmpty(areaRespDTOList)) {
            List<AreaRespDTO> treeAreaRespDTOList = buildTree(areaRespDTOList);
            return treeAreaRespDTOList;
        }
        return new ArrayList<>();
    }

    private List<AreaRespDTO> buildTree(List<AreaRespDTO> areaRespDTOList) {
        List<AreaRespDTO> treeAreaRespDTOList = new ArrayList<>();
        for(AreaRespDTO areaRespDTO:areaRespDTOList){
            if(areaRespDTO.getParentAreaId().intValue() == 0){
                areaRespDTO = buildChildTree(areaRespDTO,areaRespDTOList);
                treeAreaRespDTOList.add(areaRespDTO);
            }
        }
        return treeAreaRespDTOList;
    }

    private AreaRespDTO buildChildTree(AreaRespDTO pNode,List<AreaRespDTO> areaRespDTOList) {
        List<AreaRespDTO> childPermDTOList = new ArrayList<>();
        for(AreaRespDTO areaRespDTO : areaRespDTOList) {
            if(areaRespDTO.getParentAreaId().intValue() == pNode.getId()) {
                childPermDTOList.add(buildChildTree(areaRespDTO,areaRespDTOList));
            }
        }
        pNode.setChildAreaList(childPermDTOList);
        return pNode;
    }

    private boolean existChildArea(AreaDO areaDO) {
        return areaMapper.countChildByArea(areaDO) > 0;
    }
}
